package com.ccrego.haiyi.utils;

import android.content.Context;

import com.ccrego.haiyi.account.UrlCenter;
import com.ccrego.haiyi.common.http.TokenInfo;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by Administrator on 2017/6/16.
 */

public class WechatHelper {


    public static final String WECHAT_APP_ID = "wx8514ef5c33b41426";
    public static final String WECHAT_SECRET = "8cb2f6b2ac870c6e64805ec49a394ab2";


    private WechatHelper() {
    }

    private static WechatHelper instance;


    public static WechatHelper getInstance() {
        if (instance == null) {
            synchronized (WechatHelper.class) {
                instance = new WechatHelper();
            }
        }
        return instance;
    }

    private IWXAPI  wechatApi;

    public IWXAPI init(Context context) {
        wechatApi = WXAPIFactory.createWXAPI(context, WECHAT_APP_ID, true);
        wechatApi.registerApp(WECHAT_APP_ID);
        return wechatApi;
    }


    /**
     * @param bean
     */
//    public void pay(WeChatOrderBean bean) {
//        PayReq request = new PayReq();
//        request.appId = WECHAT_APP_ID;
//        request.partnerId = bean.getPartnerid();
//        request.prepayId = bean.getPrepayid();
//        request.packageValue = bean.getPackageX();
//        request.nonceStr = bean.getNoncestr();
//        request.timeStamp = bean.getTimestamp();
//        request.sign = bean.getSign();
//        wechatApi.sendReq(request);
//    }


    /**
//     * @param order 订单信息
     */
//    public void pay(String order) {
//        if (TextUtils.isEmpty(order)) {
//            return;
//        }
//        WeChatOrderBean orderBean = GsonUtils.string2Object(order, WeChatOrderBean.class);
//        if (null != orderBean) {
//            pay(orderBean);
//        }
//    }


    public void sendAuthWechat() {
        //登录微信
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_sdk_demo_test";
        wechatApi.sendReq(req);
    }

    public void auth(Context context, String code,DataRequestListener<TokenInfo> listener) {
        String requestUrl = UrlCenter.WX_LOGIN;
        IHttpRequest request = new HttpRequest(context);
        UrlParams params = new UrlParams();
        params.put("code",code);
        request.get(requestUrl, params, listener);
    }

}
